-module(pb_role).

-export([pack/2, pack_msg/2, unpack/2, unpack_msg/2]).


%% 请求登录
pack(1010, {Account,Password,Remark,Video}) ->
	Bin1 = pb:encode(string, Account),
	Bin2 = pb:encode(string, Password),
	Bin3 = 
		case Remark of
			undefined ->
				pb:encode(u8, 0);
			_ ->
				BinRemarkFlag = pb:encode(u8, 1),
				BinRemark= pb:encode(string, Remark),
				<<BinRemarkFlag/binary, BinRemark/binary>>
		end,
	Bin4 = 
		case Video of
			undefined ->
				pb:encode(u8, 0);
			_ ->
				BinVideoFlag = pb:encode(u8, 1),
				BinVideo= pb:encode(bin, Video),
				<<BinVideoFlag/binary, BinVideo/binary>>
		end,
	BinData = <<Bin1/binary,Bin2/binary,Bin3/binary,Bin4/binary>>,
	{ok, pb:msg(1010, BinData)};

%% 登录成功
pack(1020, {Uid,Uname,GoodsItems,Video}) ->
	Bin1 = pb:encode(u32, Uid),
	Bin2 = pb:encode(string, Uname),
	FunGoodsItems = fun(FGoodsItems, {CountAcc, BinAcc}) ->
			FBin = pb_goods:pack_msg(2010, FGoodsItems),
			{CountAcc + 1, <<BinAcc/binary, FBin/binary>>}
	end,
	{CountGoodsItems, BinGoodsItems} = lists:foldl(FunGoodsItems, {0, <<>>}, GoodsItems),
	Bin3 = pb:encode(u16, CountGoodsItems),
	Bin4 = BinGoodsItems,
	Bin5 = 
		case Video of
			undefined ->
				pb:encode(u8, 0);
			_ ->
				BinVideoFlag = pb:encode(u8, 1),
				BinVideo= pb:encode(bin, Video),
				<<BinVideoFlag/binary, BinVideo/binary>>
		end,
	BinData = <<Bin1/binary,Bin2/binary,Bin3/binary,Bin4/binary,Bin5/binary>>,
	{ok, pb:msg(1020, BinData)};

pack(_Cmd, _Data) -> 
	{error, {unknown_command, _Cmd, _Data}}.


%% 请求登录
pack_msg(1010, {Account,Password,Remark,Video}) ->
	Bin1 = pb:encode(string, Account),
	Bin2 = pb:encode(string, Password),
	Bin3 = 
		case Remark of
			undefined ->
				pb:encode(u8, 0);
			_ ->
				BinRemarkFlag = pb:encode(u8, 1),
				BinRemark= pb:encode(string, Remark),
				<<BinRemarkFlag/binary, BinRemark/binary>>
		end,
	Bin4 = 
		case Video of
			undefined ->
				pb:encode(u8, 0);
			_ ->
				BinVideoFlag = pb:encode(u8, 1),
				BinVideo= pb:encode(bin, Video),
				<<BinVideoFlag/binary, BinVideo/binary>>
		end,
	<<Bin1/binary,Bin2/binary,Bin3/binary,Bin4/binary>>;

%% 登录成功
pack_msg(1020, {Uid,Uname,GoodsItems,Video}) ->
	Bin1 = pb:encode(u32, Uid),
	Bin2 = pb:encode(string, Uname),
	FunGoodsItems = fun(FGoodsItems, {CountAcc, BinAcc}) ->
			FBin = pb_goods:pack_msg(2010, FGoodsItems),
			{CountAcc + 1, <<BinAcc/binary, FBin/binary>>}
	end,
	{CountGoodsItems, BinGoodsItems} = lists:foldl(FunGoodsItems, {0, <<>>}, GoodsItems),
	Bin3 = pb:encode(u16, CountGoodsItems),
	Bin4 = BinGoodsItems,
	Bin5 = 
		case Video of
			undefined ->
				pb:encode(u8, 0);
			_ ->
				BinVideoFlag = pb:encode(u8, 1),
				BinVideo= pb:encode(bin, Video),
				<<BinVideoFlag/binary, BinVideo/binary>>
		end,
	<<Bin1/binary,Bin2/binary,Bin3/binary,Bin4/binary,Bin5/binary>>;

pack_msg(_Cmd, _Data) -> 
	{error, {unknown_command, _Cmd, _Data}}.


%% 请求登录
unpack(1010, _Bin0) ->
	{Account, _Bin1} = pb:decode(string, _Bin0),
	{Password, _Bin2} = pb:decode(string, _Bin1),
	{RemarkFlag, _Bin3} = pb:decode(u8, _Bin2),
	{Remark, _Bin4} =
	case RemarkFlag of
		0 ->
			{undefined, _Bin3};
		1 ->
			pb:decode(string, _Bin3)
	end,
	{VideoFlag, _Bin5} = pb:decode(u8, _Bin4),
	{Video, _Bin6} =
	case VideoFlag of
		0 ->
			{undefined, _Bin5};
		1 ->
			pb:decode(bin, _Bin5)
	end,
	{ok, {Account,Password,Remark,Video}};

%% 登录成功
unpack(1020, _Bin0) ->
	{Uid, _Bin1} = pb:decode(u32, _Bin0),
	{Uname, _Bin2} = pb:decode(string, _Bin1),
	{GoodsItemsCount, _Bin3} = pb:decode(u16, _Bin2),
	FunGoodsItems = fun(_, {GoodsItemsAcc, _BinGoodsItemsAcc}) ->
				{FunGoodsItems, _BinGoodsItemsAcc2} = pb_goods:unpack_msg(2010, _BinGoodsItemsAcc),
				{[FunGoodsItems | GoodsItemsAcc], _BinGoodsItemsAcc2}
			end,
	{GoodsItemsTmp, _Bin4} = lists:foldl(FunGoodsItems, {[], _Bin3}, lists:duplicate(GoodsItemsCount, 0)),
	GoodsItems = lists:reverse(GoodsItemsTmp),
	{VideoFlag, _Bin5} = pb:decode(u8, _Bin4),
	{Video, _Bin6} =
	case VideoFlag of
		0 ->
			{undefined, _Bin5};
		1 ->
			pb:decode(bin, _Bin5)
	end,
	{ok, {Uid,Uname,GoodsItems,Video}};

unpack(_Cmd, _Bin) -> 
	{error, {unknown_command, _Cmd, _Bin}}.

%% 请求登录
unpack_msg(1010, _Bin0) ->
	{Account, _Bin1} = pb:decode(string, _Bin0),
	{Password, _Bin2} = pb:decode(string, _Bin1),
	{RemarkFlag, _Bin3} = pb:decode(u8, _Bin2),
	{Remark, _Bin4} =
	case RemarkFlag of
		0 ->
			{undefined, _Bin3};
		1 ->
			pb:decode(string, _Bin3)
	end,
	{VideoFlag, _Bin5} = pb:decode(u8, _Bin4),
	{Video, _Bin6} =
	case VideoFlag of
		0 ->
			{undefined, _Bin5};
		1 ->
			pb:decode(bin, _Bin5)
	end,
	{{Account,Password,Remark,Video},_Bin6};

%% 登录成功
unpack_msg(1020, _Bin0) ->
	{Uid, _Bin1} = pb:decode(u32, _Bin0),
	{Uname, _Bin2} = pb:decode(string, _Bin1),
	{GoodsItemsCount, _Bin3} = pb:decode(u16, _Bin2),
	FunGoodsItems = fun(_, {GoodsItemsAcc, _BinGoodsItemsAcc}) ->
				{FunGoodsItems, _BinGoodsItemsAcc2} = pb_goods:unpack_msg(2010, _BinGoodsItemsAcc),
				{[FunGoodsItems | GoodsItemsAcc], _BinGoodsItemsAcc2}
			end,
	{GoodsItemsTmp, _Bin4} = lists:foldl(FunGoodsItems, {[], _Bin3}, lists:duplicate(GoodsItemsCount, 0)),
	GoodsItems = lists:reverse(GoodsItemsTmp),
	{VideoFlag, _Bin5} = pb:decode(u8, _Bin4),
	{Video, _Bin6} =
	case VideoFlag of
		0 ->
			{undefined, _Bin5};
		1 ->
			pb:decode(bin, _Bin5)
	end,
	{{Uid,Uname,GoodsItems,Video},_Bin6};

unpack_msg(_Cmd, _Bin) -> 
	{error, {unknown_command, _Cmd, _Bin}}.
